import { jisAPP } from '../transfer/dependence'
Page({
  data: {
    url: jisAPP
  },
  onLoad(e) {
    let { currentUserType = "" } = e
    if (currentUserType) {
      this.setData({
        url: `${jisAPP}?currentUserType=1`
      })
    }
    wx.getStorage({
      key: 'USER_INFO_EXPORT',
      complete: (res) => {
        const { token = "" } = res.data || {}
        this.setData({
          url: `${this.data.url}${token ? 'individualCenter' : ''}`
        })
      }
    })
  },
  transFormPageCallback({ url = "", action = "", msg = "", code = '', params = '' }) {
    let res = {
      msg, code, params
    },
      isInclude = url.indexOf('?') > -1
    this.setData({
      url: `${url}${isInclude ? '&' : '?'}action=${action}&result=${JSON.stringify(res)}&timestamp=` + new Date().getTime()
    })
  },
  webListener(e) {
    const [{ action = "", params = {} }] = (e.detail && e.detail.data) || [],
      pages = getCurrentPages(),
      prevPage = pages[pages.length - 2]
    if (action == 'loginApp') {
      while (!wx.getStorageSync('USER_INFO_EXPORT')) {
        wx.setStorageSync('USER_INFO_EXPORT', params)
      }
      prevPage.initLoginInfo()
    } else if (action == 'loginOut') {
      while (wx.getStorageSync('USER_INFO_EXPORT')) {
        wx.removeStorageSync('USER_INFO_EXPORT')
      }
      prevPage.initLoginInfo()
    }
  }
})
